package cc.mcyx.ychat.service.impl

import cc.mcyx.ychat.mapper.UserMapper
import cc.mcyx.ychat.model.User
import cc.mcyx.ychat.service.UserService
import cn.hutool.jwt.JWT
import com.mybatisflex.spring.service.impl.ServiceImpl
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import javax.servlet.http.HttpServletRequest

@Service
open class UserServiceImpl : ServiceImpl<UserMapper, User>(), UserService {

    @Autowired
    lateinit var httpServletRequest: HttpServletRequest

    @Autowired
    lateinit var userService: UserService

    override fun getCurrentUser(): User {
        httpServletRequest.getHeader("Authorization")?.let { token ->
            val uid: Int = JWT.of(token).getPayload("uid").toString().toInt()
            return userService.getById(uid)
        }
        throw RuntimeException("Token is invalid")
    }
}